<?php
class GroupController extends IndexController
{
    public function init()
    {
		parent::init();
        $this->utl             = Yaf_Registry::get("utl");
        $this->GroupModel      = new GroupModel();
        $this->AuthorityModel  = new AuthorityModel();
        $this->UserLogsModel   = new LogsModel();
        $this->Session         = Yaf_Registry::get("session");
    }

    public function listAction()
    {
        $allAuthority_info = $this->AuthorityModel->selectAuthority();
        $offset            = 10;
        $page              = $this->getRequest()->getParam("page",1);
        $startLimit        = $offset*($page-1);
        $allGroup_info     = $this->GroupModel ->getGroupInfo($startLimit,$offset);
        $allCounts         = $this->GroupModel ->getAllCount();
        $allpage           = $this->utl::page('/Group/list', $page, $allCounts[0]['rows'], $offset, $status='');
        $allGroup_list     = array();
        foreach ($allGroup_info AS $k => $v)
        {
            $allGroup_list[$k]               = $v;
            $allGroup_list[$k]['rows']       = $allCounts[0]['rows'];
            $allGroup_list[$k]['every_page'] = $offset;
        }
        $this->getView()->assign("group_list",$allGroup_list);
        $this->getView()->assign("authority_list",$allAuthority_info);
        $this->getView()->assign("allpage",$allpage);
        $authority_id_array = array();
        foreach ($allGroup_info AS $k => $v)
        {
            $authority_id_array[$v['group_id']][] = array('groupd_id' => $v['group_id'], 'authority' => $this->GroupModel->getGroupAuthorityID($v['group_id']));
        }
        if(empty($this->Session->user_info))
        {
            $this->redirect('/User/login');
            return true;
        }
        $this->getView()->assign('group_authority', $authority_id_array);
    }

    public function pageAction()
    {
        $allAuthority_info = $this->AuthorityModel->selectAuthority();
        $offset            = 10;
        $page              = $this->getRequest()->getParam("page",1);
        $startLimit        = $offset*($page-1);
        $allGroup_info     = $this->GroupModel ->getGroupInfo($startLimit,$offset);
        $allCounts         = $this->GroupModel ->getAllCount();
        $allpage           = $this->utl::page('/Group/list', $page, $allCounts[0]['rows'], $offset, $status='');
        $this->getView()->assign("group_list",$allGroup_info);
        $this->getView()->assign("authority_list",$allAuthority_info);
        $authority_id_array = array();
        foreach ($allGroup_info AS $k => $v)
        {
            $authority_id_array[$v['group_id']][] = array('groupd_id' => $v['group_id'], 'authority' => $this->GroupModel->getGroupAuthorityID($v['group_id']));
        }
        $this->getView()->assign('group_authority', $authority_id_array);
        exit(json_encode($allGroup_info, true));
    }

    public function currentPageAction()
    {
        $offset            = 10;
        $page              = $this->getRequest()->getParam("page",1);
        $startLimit        = $offset*($page-1);
        $allGroup_info     = $this->GroupModel ->getGroupInfo($startLimit,$offset);
        $allCounts         = $this->GroupModel ->getAllCount();
        $allpage           = $this->utl::page('/Group/list', $page, $allCounts[0]['rows'], $offset, $status='');
        exit($allpage);
    }

    public function delAction()
    {
        $id = $this->getRequest()->getParam('id');
        if (empty($id))
        {
            $this->forward('Group', 'list');
            return;
        }
        $rs          = $this->GroupModel->del($id);
        $session_del = array();
        $user_info   = $this->Session->user_info;
        foreach ($user_info as $k => $v) 
        {
            $session_del[$k]['user_id']    = $v['user_id'];
            $session_del[$k]['table_name'] = "group";
            $session_del[$k]['action']     = "del";
        }
        $this->UserLogsModel->addUser($session_del);
        header('Content-type:application/json;charset:utf8');
        if (false === $rs)
        {
            exit('{"status":false,"info":"删除失败！"}');
            return;
        }
        exit('{"status":true,"info":"删除成功"}');
        return true;
    }

    public function editAction()
    {
        $id = $this->getRequest()->getParam('id');
        if (empty($id))
        {
            $this->forward('Group', 'list');
            return;
        }
        if ($this->getRequest()->isPost())
        {
            $post = $this->getRequest()->getPost();
            header('Content-type:application/json;charset:utf8');

            $rs = $this->GroupModel->edit($post, $id);
            foreach($post as $val)
            {
                foreach($val as $d)
                {
                    $res2 = $this->GroupModel->editID($d,$id);
                }
            }
            $session_edit = array();
            $user_info    = $this->Session->user_info;
            foreach ($user_info as $k => $v) 
            {
                $session_edit[$k]['user_id']    = $v['user_id'];
                $session_edit[$k]['table_name'] = "group";
                $session_edit[$k]['action']     = "edit";
            }
            $this->UserLogsModel->addUser($session_edit);

            if (false === $rs||false === $res2)
            {
                exit('{"status":false,"info":"编辑失败！"}');
                return;
            }
            exit('{"status":true,"info":"编辑成功！"}');
        }
    }

    public function addAction()
    {
        if ($this->getRequest()->isPost())
        {
            $post = $this->getRequest()->getPost();
            header('Content-type:application/json;charset:utf8');
            $rs   = $this->GroupModel->add($post);
            //获取最后一条id
            $lastId=$this->GroupModel->selectLastId();
            foreach($post as $val)
            {
                foreach($val as $a)
                {
                    $res2 = $this->GroupModel->addId($lastId,$a);
                }
            }
            $session_add = array();
            $user_info   = $this->Session->user_info;
            foreach ($user_info as $k => $v) 
            {
                $session_add[$k]['user_id']    = $v['user_id'];
                $session_add[$k]['table_name'] = "group";
                $session_add[$k]['action']     = "add";
            }
            $this->UserLogsModel->addUser($session_add);

            if (false === $rs||false === $res2)
            {
                exit('{"status":false,"info":"添加失败！"}');
                return;
            }
            exit('{"status":true,"info":"添加成功！"}');
        }
    }
}
?>